//
//  NavigationTitleView.m
//  18Birdies
//
//  Created by Lc on 15/7/7.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "NavigationTitleView.h"

@implementation NavigationTitleView

+ (NavigationTitleView*)navigationTitleViewWithTitle:(NSString*)title lcFont:(LcFont*)lcFont {
  //  LC_CHECK_NOT_EMPTY(title);
  if ([title trim].length > 50) {
    title = [[title trim] substringToIndex:50];
  }
  NavigationTitleView* navigationTitleView =
      [[NSBundle mainBundle] loadNibNamed:@"NavigationTitleView" owner:nil options:nil][0];
  // hard coded here for simplicity
  navigationTitleView.frame = self.navigationTitleFrame;
  [navigationTitleView.centerTitleLable setLcFont:lcFont];
  // intrinsicContentSize invalid in iOS 11, so here limit width for simplicity
  NSString* formatTitle;
  CGFloat maxWidth = self.navigationTitleFrame.size.width;
  if ([title widthWithLcFont:lcFont] > self.navigationTitleFrame.size.width &&
      [UIDevice isSmallUISize]) {
    LcFont* smallerFont = [self fontFitTitle:title lcFont:lcFont];
    formatTitle = [title formatEllipsisWithWidthLimit:maxWidth lcFont:smallerFont];
    [navigationTitleView.centerTitleLable setLcFont:smallerFont];
  } else {
    formatTitle = [title formatEllipsisWithWidthLimit:maxWidth lcFont:lcFont];
  }
  navigationTitleView.centerTitleLable.text = formatTitle;
  [navigationTitleView hiddenSubTitle:YES];
  return navigationTitleView;
}

+ (LcFont*)fontFitTitle:(NSString*)title lcFont:(LcFont*)lcFont {
  CGFloat size = lcFont.size;
  if (size < 14) {
    return lcFont;
  }
  CGFloat maxWidth = self.navigationTitleFrame.size.width;
  for (int i = size; i > 14; i--) {
    LcFont* smallerFont = lcFont.fontSize(i);
    if ([title widthWithLcFont:smallerFont] <= maxWidth) {
      return smallerFont;
    }
  }
  return lcFont;
}

+ (NavigationTitleView*)navigationTitleViewWithAttributedTitle:(NSAttributedString*)title {
  LC_CHECK_NOT_EMPTY(title);
  NavigationTitleView* navigationTitleView =
  [[NSBundle mainBundle] loadNibNamed:@"NavigationTitleView" owner:nil options:nil][0];
  // hard coded here for simplicity
  navigationTitleView.frame = self.navigationTitleFrame;
  navigationTitleView.centerTitleLable.attributedText = title;
  return navigationTitleView;
}

+ (NavigationTitleView*)navigationTitleViewWithTitle:(NSString*)title
                                            subtitle:(NSAttributedString*)subtitle {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_EMPTY(subtitle);
  NavigationTitleView* navigationTitleView =
      [[NSBundle mainBundle] loadNibNamed:@"NavigationTitleView" owner:nil options:nil][0];
  // hard coded here for simplicity
  navigationTitleView.frame = self.navigationTitleFrame;
  LcFont* lcFont = [LcFont db17];
  [navigationTitleView.titleLabel setLcFont:lcFont];
  navigationTitleView.titleLabel.text =
      [title formatEllipsisWithWidthLimit:self.navigationTitleFrame.size.width lcFont:lcFont];
  navigationTitleView.subtitleLabel.attributedText = subtitle;
  [navigationTitleView hiddenSubTitle:NO];

  return navigationTitleView;
}

+ (NavigationTitleView*)navigationTitleViewWithLightContentTitle:(NSString*)title
                                                        subtitle:(NSAttributedString*)subtitle {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_EMPTY(subtitle);
  NavigationTitleView* navigationTitleView =
      [[NSBundle mainBundle] loadNibNamed:@"NavigationTitleView" owner:nil options:nil][0];
  // hard coded here for simplicity
  navigationTitleView.frame = self.navigationTitleFrame;
  LcFont* lcFont = [LcFont wb17];
  [navigationTitleView.titleLabel setLcFont:lcFont];

  navigationTitleView.titleLabel.text =
      [title formatEllipsisWithWidthLimit:self.navigationTitleFrame.size.width lcFont:lcFont];
  navigationTitleView.subtitleLabel.attributedText = subtitle;
  [navigationTitleView hiddenSubTitle:NO];

  return navigationTitleView;
}

+ (NavigationTitleView*)
navigationTitleViewWithLightAttributedContentTitle:(NSAttributedString*)title
                                          subtitle:(NSAttributedString*)subtitle {
  LC_CHECK_NOT_EMPTY(title);
  LC_CHECK_NOT_EMPTY(subtitle);
  NavigationTitleView* navigationTitleView =
      [[NSBundle mainBundle] loadNibNamed:@"NavigationTitleView" owner:nil options:nil][0];
  // hard coded here for simplicity
  navigationTitleView.frame = self.navigationTitleFrame;
  navigationTitleView.titleLabel.attributedText = title;
  navigationTitleView.subtitleLabel.attributedText = subtitle;
  [navigationTitleView hiddenSubTitle:NO];

  return navigationTitleView;
}

- (void)hiddenSubTitle:(BOOL)hidden {
  self.titleLabel.hidden = hidden;
  self.subtitleLabel.hidden = hidden;
  self.centerTitleLable.hidden = !hidden;
}

+ (CGRect)navigationTitleFrame {
  CGFloat titleWith = LcUIUtility.screenWidth - 140;
  return CGRectMake(0, 0, titleWith, 44);
}

- (CGSize)intrinsicContentSize {
  return [NavigationTitleView navigationTitleFrame].size;
}

- (void)setTitle:(NSString*)title lcFont:(LcFont*)lcFont {
  self.centerTitleLable.text =
      [title formatEllipsisWithWidthLimit:NavigationTitleView.navigationTitleFrame.size.width
                                   lcFont:lcFont];
  [self.centerTitleLable setLcFont:lcFont];
}

- (void)setTitle:(NSString*)title
       titleFont:(LcFont*)titleFont
        subtitle:(NSAttributedString*)subtitle {
  [self.titleLabel setLcFont:titleFont];
  self.titleLabel.text =
      [title formatEllipsisWithWidthLimit:NavigationTitleView.navigationTitleFrame.size.width
                                   lcFont:titleFont];
  self.subtitleLabel.attributedText = subtitle;
}

- (void)setAttributedTitleTitle:(NSAttributedString*)title {
  [self.centerTitleLable setAttributedText:title];
}

@end
